﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer.FindArtInfo;
using CommandLib;
using FindArtInfo.Common;
using FindArtInfo.Models;
using Resources;

namespace FindArtInfo.Controllers
{
    public class MonogramController : MultiBase
    {
        //
        // GET: /Monogram/
        public ActionResult Search()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(PageMonogramSearchModel model)
        {
            if(string.IsNullOrEmpty(model.KeyWord))
            {
                ModelState.AddModelError("KeyWord", Multi.PageMonogramSearchMessageError);
                return View();
            }
            return
                Redirect(string.Format("~/{0}/Monogram/Result?keyWord={1}",
                                       Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE)),
                                       model.KeyWord));
        }

        public ActionResult Result(string keyWord, int? pageIndex)
        {
            int pageSize = 30;
            int totalRecord;
            PageMonogramSearchResultModel model = new PageMonogramSearchResultModel();
            model.PageIndex = pageIndex == null ? 1 : Convert.ToInt32(pageIndex);
            model.PageSize = pageSize;
            model.KeyWord = keyWord;
            model.ListMonogram = new MonogramTextBL().Search(keyWord, Convert.ToInt32(pageIndex == null ? 1 : pageIndex),
                                                      pageSize, out totalRecord);
            model.TotalRecord = totalRecord;
            model.TotalPage = model.TotalRecord % model.PageSize == 0
                                  ? model.TotalRecord / model.PageSize
                                  : (model.TotalRecord / model.PageSize) + 1;
            model.Url = string.Format("/{0}/{1}?keyWord={2}", "Monogram", "Result",
                                      keyWord);
            return View(model);
        }

        public ActionResult Help()
        {
            return View();
        }

    }
}
